package com.hp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.entity.Department;
import com.hp.entity.Employee;
import com.hp.entity.Position;
import com.hp.service.DepartmentService;
import com.hp.service.EmployeeService;
import com.hp.service.PositionService;
import com.hp.util.MTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private PositionService positionService;


    /*跳转到分页页面*/
    @RequestMapping("/listPage")
    public String listPage(Model model, @RequestParam(value = "current",defaultValue = "1") Integer current, @RequestParam(value = "size",defaultValue = "3") Integer size){
        Page<Employee>  employeePage =employeeService.selectListByPage(current);
        List<Employee> records = employeePage.getRecords();
        model.addAttribute("employeePage",employeePage);
	    model.addAttribute("records",records);
        return "employee/employee_list";
    }

    /*跳转到添加页面*/
    @RequestMapping("/toadd")
    public String toadd(Model model){
        //查询最大的员工编号
        Integer maxNum = employeeService.selectMaxNum();
        //查询部门列表
        List<Department> departmentsList = departmentService.list(new QueryWrapper<>());
        //查询职位列表
        List<Position> positionList = positionService.list(new QueryWrapper<>());
        model.addAttribute("employeeNumber",maxNum==null?1001:maxNum+1);
        model.addAttribute("departments", departmentsList);
        model.addAttribute("position",positionList);
        return "employee/employee_add";
    }

    /*添加方法*/
    @RequestMapping("/addEmployee")
    public String addEmployee(Employee employee,Model model,String date,String Onetime){
        employee.setBirthday(MTimeUtil.stringParse(date));
        employee.setInTime(MTimeUtil.stringParse(Onetime));
        boolean insert = employeeService.save(employee);
        return insert ? "redirect:/employee/listPage": "employee/employee_add";
    }

    /*跳转到查看员工信息页面*/
    @RequestMapping("/selEmployee/{id}")
    public String selectEmployee(Model model,@PathVariable("id") Integer id){
        System.out.println(id);
        Employee employee = employeeService.getById(id);
        employee = employeeService.selectEmployee(employee);
        model.addAttribute("employee", employee);
        return "employee/employee_detail";
    }

    /*跳转到修改页面*/
    @RequestMapping("/toUpdate/{id}")
    public String toUpdete(Model model,@PathVariable("id") Integer id){

        // 根据主键id返回对象
        Employee employee = employeeService.getById(id);
        employee = employeeService.selectEmployee(employee);
        //查询部门列表
        List<Department> departmentsList = departmentService.list(new QueryWrapper<>());
        model.addAttribute("departments", departmentsList);
        //查询职位列表
        List<Position> positionList = positionService.list(new QueryWrapper<>());
        model.addAttribute("positions",positionList);
        model.addAttribute("employee",employee);
        return  "/employee/employee_update";
    }

    /*修改员工信息方法*/
    @RequestMapping("/updEmployee")
    public String updEmployee(Model model,Employee employee,String date,String Onetime){
        employee.setBirthday(MTimeUtil.stringParse(date));
        employee.setInTime(MTimeUtil.stringParse(Onetime));
        boolean b = employeeService.updateById(employee);
        return b?"redirect:/employee/listPage":"employee/employee_update";

    }


    /*删除员工信息的方法*/
    @RequestMapping("/delEmployee/{id}")
    public String delEmployee( Model model,@PathVariable("id") Integer id){
        boolean b = employeeService.removeById(id);
        if (b){
            model.addAttribute("err","删除成功");
        }else {
            model.addAttribute("err","删除失败");
        }
        return "redirect:/employee/listPage";
    }
}
